<?php
/**
  *根据Tp重写微擎数据库操作
  *@author MIn晓天
  *wx:416883181
  */
defined('IN_IA') or exit('Access Denied');
use think\Db;


function pdo_query($sql, $params = array()) {
    return Db::query($sql, $params);
}


function pdo_fetchcolumn($sql, $params = array(), $column = 0) {
    $param = array();
    foreach ($params as $k=>$v){
        $arr_k = str_replace(':','',$k);
        $param[$arr_k] = $v;
    }
    $data = Db::query($sql,$param);
    if(!empty($data)){
        return $data[0]['COUNT(*)'];
    }
    return $data;
}

function pdo_fetch($sql, $params = array()) {
    $param = array();
    foreach ($params as $k=>$v){
        $arr_k = str_replace(':','',$k);
        $param[$arr_k] = $v;
    }
    $data = Db::query($sql,$param);
    if(!empty($data)){
        return $data[0];
    }
    return $data;
}

function pdo_fetchall($sql, $params = array(), $keyfield = '') {
    $param = array();
    foreach ($params as $k=>$v){
        $arr_k = str_replace(':','',$k);
        $param[$arr_k] = $v;
    }
    return Db::query($sql, $param);;
}


function pdo_get($tablename, $condition = array(), $fields = array()) {
    return   Db::name($tablename)->where($condition)->field($fields)->find();
}

function pdo_getall($tablename, $condition = array(), $fields = array(), $keyfield = '', $orderby = array(), $limit = array()) {
    $db = Db::name($tablename);
    if(!empty($condition)){
        $db->where($condition);
    }
    if(!empty($fields)){
        $db->field($fields);
    }
    //暂留
    if(!empty($keyfield)){

    }
    if(!empty($orderby)){
        $db->order($orderby);
    }
    if(!empty($limit)&&is_array($limit)&&count($limit)<=2){
        $offset = '';
        foreach ($limit as $k){
           $offset .=$k.",";
        }
        $offset = rtrim($offset,',');
        $db->limit($offset);
    }
    $data =  $db->select()->toArray();
    return   $data;
}

function pdo_getslice($tablename, $condition = array(), $limit = array(), &$total = null, $fields = array(), $keyfield = '', $orderby = array()) {




}

function pdo_getcolumn($tablename, $condition = array(), $field) {




}


function pdo_exists($tablename, $condition = array()) {


}


function pdo_count($tablename, $condition = array(), $cachetime = 15) {




}


function pdo_update($table, $data = array(), $params = array(), $glue = 'AND') {
    $data = Db::name($table)->where($params)->update($data);
    return $data;
}


function pdo_insert($table, $data = array(), $replace = FALSE) {
    //var_dump($table);die();
    $_SESSION['lasttable'] = $table;
    $data = Db::name($table)->insert($data);
    return $data;
}


function pdo_delete($table, $params = array(), $glue = 'AND') {
    $data = Db::name($table)->where($params)->delete();
    return $data;
}


function pdo_insertid() {
    $table = $_SESSION['lasttable'];
    $insID = Db::name($table)->getLastInsID();
    return $insID;
}


function pdo_begin() {
    return false;
}


function pdo_commit() {
    return false;
}


function pdo_rollback() {
    return false;
}


function pdo_debug($output = true, $append = array()) {
	$sql = Db::getLastSql();
	exit($sql);
}

function pdo_run($sql) {
    return Db::execute($sql);
}


function pdo_fieldexists($tablename, $fieldname = '') {
	return false;
}

function pdo_fieldmatch($tablename, $fieldname, $datatype = '', $length = '') {
    return false;
}

function pdo_indexexists($tablename, $indexname = '') {
    return false;
}


function pdo_fetchallfields($tablename){
	$fields = pdo_fetchall("DESCRIBE {$tablename}", array(), 'Field');
	$fields = array_keys($fields);
	return $fields;
}


function pdo_tableexists($tablename){
    return false;
}
